/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package donnees;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.rmi.server.Dispatcher;

/**
 *
 * @author cynthia
 */
public class UserDAO {

    private String madb = "javaweb", user = "javaweb", mdp = "javaweb";
    private Connection connect = null;
    private PreparedStatement stmt = null;
    private Statement statement, statementTemp;
    private ResultSet resultSet, resultSetTemp;

    //Connexion a la base de donnees derby
    public void connect() {
        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Dispatcher.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {

            connect = DriverManager.getConnection("jdbc:derby://localhost:1527/" + madb + ";user=" + user + ";password=" + mdp);
            System.out.println("Connexion OK");
        } catch (SQLException ex) {
            Logger.getLogger(Dispatcher.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    
    public boolean validateUser(String login, String password) {
        boolean valid = false;
        try {
                connect();
                statement = connect.createStatement();
                resultSet = statement.executeQuery("select * from "+madb+".users");
                while(resultSet.next()){
                    if(login.equals(resultSet.getString("login")) && password.equals(resultSet.getString("password")))
                        valid=true;
                    }
            } catch (Exception ex) {
                System.out.println("Erreur BDD");
            }
            finally {
                close();
                return valid;
            }
//            
//            
//            stmt = connect.prepareStatement("select id_users from users where login=? and password=?");
//            stmt.executeUpdate();
//            stmt.setString(1, login);
//            stmt.setString(2, password);
//
//            System.err.println(login);
//
//            String[] fields = {"id_user"};
//            ArrayList result = Servlet.Authentification.query(stmt, fields);
//            if (result.size() == 1) {
//                valid = true;
//            }

    }
     //fonction qui ferment les connexions ouverte avec la base de données
    private void close() {
            try {
                    if (resultSet != null) {
                            resultSet.close();
                    }
                    if (statement != null) {
                            statement.close();
                    }

                    if (connect != null) {
                            connect.close();
                    }

            } catch (Exception e) {

            }
    }
}
